package com.luxuewen.sxa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luxuewen.sxa.common.annotation.ApiAuth;
import com.luxuewen.sxa.entity.SxaRoles;
import com.luxuewen.sxa.entity.validator.SetPermissionVo;
import com.luxuewen.sxa.service.impl.SxaRolesServiceImpl;
import com.luxuewen.sxa.utils.response.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author wen
 */
@RestController
@Validated
@Slf4j
public class RoleController {
    @Autowired
    private SxaRolesServiceImpl sxaRolesService;

    @ApiAuth(alias = "role:create")
    @PostMapping("role/create")
    public ApiResponse<SxaRoles> create (@Validated @RequestBody SxaRoles sxaRoles) {
        QueryWrapper<SxaRoles> wrapper = new QueryWrapper<>();
        wrapper.eq("alias", sxaRoles.getAlias());
        boolean ex = sxaRolesService.exists(wrapper);
        if (ex) {
            return ApiResponse.fail("角色别名已存在，请勿重复设置！");
        }

        boolean res = sxaRolesService.save(sxaRoles);
        if (!res) {
            return ApiResponse.fail("角色添加失败！");
        }

        return ApiResponse.success(sxaRoles);
    }

    @ApiAuth(alias = "role:update")
    @PostMapping("role/update")
    public ApiResponse<SxaRoles> update (@Validated @RequestBody SxaRoles sxaRoles) {
        QueryWrapper<SxaRoles> wrapper = new QueryWrapper<>();
        wrapper.ne("id", sxaRoles.getId())
                .eq("alias", sxaRoles.getAlias());
        boolean ex = sxaRolesService.exists(wrapper);
        if (ex) {
            return ApiResponse.fail("角色别名已存在，请勿重复设置！");
        }

        boolean res = sxaRolesService.updateById(sxaRoles);
        if (!res) {
            return ApiResponse.fail("角色保存失败！");
        }

        return ApiResponse.success(sxaRoles);
    }

    @ApiAuth(alias = "role:delete")
    @PostMapping("role/delete/{id}")
    public ApiResponse<Boolean> delete (@PathVariable Integer id) {
        boolean res = sxaRolesService.removeById(id);
        if (!res) {
            return ApiResponse.fail("角色删除失败！");
        }

        return ApiResponse.success(res);
    }

    @ApiAuth(alias = "roles:list")
    @GetMapping("role/list/{page}/{limit}")
    public ApiResponse<Page<Map<String, Object>>> list (
            @PathVariable Integer page,
            @PathVariable Integer limit,
            @RequestParam String search) {
        Page<Map<String, Object>> ePage = new Page<>(page, limit);

        QueryWrapper<SxaRoles> sxaRolesQueryWrapper = new QueryWrapper<>();
        if (!search.isEmpty()) {
            sxaRolesQueryWrapper.like("name", search);
        }

        return ApiResponse.success(sxaRolesService.pageMaps(ePage, sxaRolesQueryWrapper));
    }

    @ApiAuth(alias = "role:options")
    @GetMapping("role/options")
    public ApiResponse<List<Map<String, Object>>> options () {
        QueryWrapper<SxaRoles> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1).select("id", "name", "alias");

        return ApiResponse.success(sxaRolesService.listMaps(wrapper));
    }

    @ApiAuth(alias = "role:set:permissions")
    @PostMapping("role/set/permissions")
    public ApiResponse<Boolean> setPermissions (@Validated @RequestBody SetPermissionVo setPermissionVo) {
        return ApiResponse.success(sxaRolesService.setPermission(setPermissionVo));
    }
}
